package service;

import beans.Employee;
import db.EmployeeDAO;
import java.sql.SQLException;

public class EmployeeService {
    private EmployeeDAO employeeDAO = new EmployeeDAO();

    // 验证员工登录
    public Employee validateEmployeeLogin(String username, String password) {
        System.out.println("EmployeeService - 验证员工登录: " + username);
        Employee employee = employeeDAO.validateEmployeeLogin(username, password);
        if (employee != null) {
            System.out.println("EmployeeService - 员工登录验证成功");
        } else {
            System.out.println("EmployeeService - 员工登录验证失败");
        }
        return employee;
    }

    // 验证司机队长登录
    public Employee validateDriverLeaderLogin(String username, String password) {
        System.out.println("EmployeeService - 验证司机队长登录: " + username);
        Employee employee = employeeDAO.validateEmployeeLogin(username, password);
        if (employee != null) {
            System.out.println("EmployeeService - 司机队长登录验证成功，部门ID: " + employee.getDepartmentID());
            if (employee.getDepartmentID() == 4) {
                return employee;
            } else {
                System.out.println("EmployeeService - 司机队长登录失败：部门ID不匹配");
            }
        } else {
            System.out.println("EmployeeService - 司机队长登录验证失败");
        }
        return null;
    }

    // 验证财务登录
    public Employee validateFinanceLogin(String username, String password) {
        System.out.println("EmployeeService - 验证财务登录: " + username);
        Employee employee = employeeDAO.validateEmployeeLogin(username, password);
        if (employee != null) {
            System.out.println("EmployeeService - 财务登录验证成功，部门ID: " + employee.getDepartmentID());
            if (employee.getDepartmentID() == 1) {
                return employee;
            } else {
                System.out.println("EmployeeService - 财务登录失败：部门ID不匹配");
            }
        } else {
            System.out.println("EmployeeService - 财务登录验证失败");
        }
        return null;
    }

    // 验证普通员工登录
    public Employee validateNormalEmployeeLogin(String username, String password) {
        System.out.println("EmployeeService - 验证普通员工登录: " + username);
        return employeeDAO.validateEmployeeLogin(username, password);
    }

    // 添加员工
    public int addEmployee(Employee employee) {
        return employeeDAO.addEmployee(employee);
    }

    // 更新员工信息
    public int updateEmployee(Employee employee) {
        return employeeDAO.updateEmployee(employee);
    }

    // 根据ID获取员工信息
    public Employee getEmployeeByID(int employeeID) {
        return employeeDAO.getEmployeeByID(employeeID);
    }

    // 新增方法：修改密码
    public boolean changePassword(int employeeId, String oldPassword, String newPassword) throws SQLException {
        // 1. 验证原密码
        Employee employee = employeeDAO.getEmployeeByID(employeeId);
        if (employee == null || !employee.getPassword().equals(oldPassword)) {
            return false; // 原密码不正确或员工不存在
        }

        // 2. 更新密码
        // 假设 EmployeeDAO 中有一个 updatePassword(int employeeId, String newPassword) 方法
        return employeeDAO.updatePassword(employeeId, newPassword);
    }
} 